package com.taobao.client.pages;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.Data;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.util.PopBox;

public class PriceChange extends Composite {

	public static input input;

	public static void initOrder(String bizOrderId) {
		input = new input();
		input.bizOrderId = bizOrderId;
		input.bizType = Data.bizType;
		input.url = Window.Location.getHref();
	};

	static class input {
		String bizType;// Int 是 业务类型
		String bizOrderId;// Long 是 业务订单id
		String url;// url 是 回调url

		Map<String, String> getParam() {
			HashMap<String, String> m = new HashMap<String, String>();
			m.put("method", Data.PRICECHANGE);
			m.put("biz_type", bizType);
			m.put("biz_order_id", bizOrderId);
			
			url = url.substring(0,url.indexOf("#"));
			m.put("callback_url", url);
			return m;
		}
	}

	class output {
		String sumDiscountAdjustFee;// String 否 成功则返回修改的价格
		String ResultCode;// String 否 执行修改价格失败，返回失败原因
		String bizOrderId;// Long 是 业务订单id

		output() {
			String queryString = Window.Location.getQueryString();
			String[] split = queryString.split("&");
			for (String sp : split) {
				String[] split2 = sp.split("=");

				if (split2[0].indexOf("sumDiscountAdjustFee") >= 0) {
					sumDiscountAdjustFee = split2[1];
				}
				if (split2[0].indexOf("ResultCode") >= 0) {
					ResultCode = split2[1];
				}
				if (split2[0].indexOf("bizOrderId") >= 0) {
					bizOrderId = split2[1];
				}
			}
		}
	}

	public PriceChange() {
//		if (!HISTORY.equals(History.getToken()))
//			History.newItem(HISTORY);

		String queryString = Window.Location.getQueryString();

		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			processOutput();
		} else
			processInput();
	}

	private void processInput() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("change price"));

		f = new Frame();
		content.add(f);

		if (input == null) {
			content.add(new Label("非法进入,请重新进入"));
		} else
			RPC.getService().getUrl(input.getParam(), inputCallBack);

		initWidget(content);
	}

	AsyncCallback<String> inputCallBack = new AsyncCallback<String>() {
		@Override
		public void onFailure(Throwable arg0) {
			PopBox.error("构造url出错", arg0.getMessage());
		}

		@Override
		public void onSuccess(String arg0) {
//			f.setUrl(arg0);
//			f.setSize(Data.width, Data.height);
			PopBox.openWindow(arg0);
		}
	};

	private void processOutput() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("change price response"));
		output o = new output();

		content.add(new Label("ResultCode = " + o.ResultCode));
		content.add(new Label("sumDiscountAdjustFee修改的价格 = "
				+ o.sumDiscountAdjustFee));

		initWidget(content);
	}

	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}

	public static PriceChange getPriceChagePanel() {
		if (price == null) {
			price = new PriceChange();
		}
		return price;
	}

	static PriceChange price;
	Frame f;
	static final String RESULT_FLAG = "ResultCode";
	public final static String HISTORY = "price";
}
